<?php

namespace App\Http\Controllers\Web\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;

class FaceController extends ApiController
{
    public function faceDetect(Request $request)
    {
        $api = '/api/v1/face/detectface';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function getEmotion(Request $request)
    {
        $api = '/api/v1/face/getemotion';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function getLandmarks(Request $request)
    {
        $api = '/api/v1/face/getlandmarks';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function getAgeGender(Request $request)
    {
        $api = '/api/v1/face/getagegender';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function textureSwap(Request $request)
    {
        $api = '/api/v1/face/texture_swap_3d';

        if($request->hasFile('image0') && $request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else if($request->hasFile('image0'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => $request->get('image1'),
            );
        }else if($request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => $request->get('image1'),
            );
        }

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function swapFace(Request $request)
    {
        $api = '/api/v1/face/swapface';

        if($request->hasFile('image0') && $request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else if($request->hasFile('image0'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => $request->get('image1'),
            );
        }else if($request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => $request->get('image1'),
            );
        }

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function generateModel(Request $request)
    {
        $api = '/api/v1/face/generate_3d_model';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function makeupSwap(Request $request)
    {
        $api = '/api/v1/face/makeupswap';

        if($request->hasFile('image0') && $request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else if($request->hasFile('image0'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => $request->get('image1'),
            );
        }else if($request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => $request->get('image1'),
            );
        }

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function faceAging(Request $request)
    {
        $api = '/api/v1/face/faceaging';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
            'value' => $request->get('value'),
        );
        // return $post_data;

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function detectHead(Request $request)
    {
        $api = '/api/v1/face/detecthead';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
            'minsize' => $request->get('minsize')
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function faceCompare(Request $request)
    {
        $api = '/api/v1/face/facecompare';

        if($request->hasFile('image0') && $request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else if($request->hasFile('image0'))
        {
            $post_data = array(
                'image0' => self::imgtobase64($request->file('image0')),
                'image1' => $request->get('image1'),
            );
        }else if($request->hasFile('image1'))
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => self::imgtobase64($request->file('image1')),
            );
        }else
        {
            $post_data = array(
                'image0' => $request->get('image0'),
                'image1' => $request->get('image1'),
            );
        }

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function hairSegement(Request $request)
    {
        $api = '/api/v1/face/hairsegement';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function hairColor(Request $request)
    {
        $api = '/api/v1/face/haircolor';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image,
            'r' => $request->get('r'),
            'g' => $request->get('g'),
            'b' => $request->get('b')
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function beautyRank(Request $request)
    {
        $api = '/api/v1/face/beautyrank';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    public function skinClassify(Request $request)
    {
        $api = '/api/v1/face/skinclassify';

        if($request->hasFile('image'))
        {
            $image = self::imgtobase64($request->file('image'));
        }else
        {
            $image = $request->image;
        }
        $post_data = array(
            'image' => $image
        );

        $face_data = $this->postCurl($api, $post_data);
        return $face_data;
    }

    protected function postCurl($api, $post_data)
    {
        $host = config('pkx.face_core_host');
        $post_url = $host.$api;

        $ch = \curl_init();
        \curl_setopt($ch, CURLOPT_URL, $post_url);
        \curl_setopt($ch, CURLOPT_HEADER, 0);
        \curl_setopt($ch, CURLOPT_POST, 1);
        \curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = \curl_exec($ch);
        \curl_close($ch);
        return $output;
    }

    public function imgtobase64($img='', $imgHtmlCode=true)
    {
        $imageInfo = getimagesize($img);
        $base64 = "" . chunk_split(base64_encode(file_get_contents($img)));
        return 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents($img)));
    }
}
